//
//  LYVisitorView.swift
//  Swift---新浪微博练习代码
//
//  Created by Mr.Yan on 2017/6/30.
//  Copyright © 2017年 Mr.Yan. All rights reserved.
//

import UIKit
import SnapKit
class LYVisitorView: UIView {
//01声明闭包
    var closure:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //提供一个外界可以使用的方法(设置消息 发现 我的logo 文字 但是还要区分首页的情况)
    //首页多一个圆圈动画 其他的没有
    func setupVisitorViewInfo(imageName:String?, title:String?){
    
        if let img = imageName, let tit = title {
            logoImageView.image = UIImage(named:img)
            messageLabel.text = tit
            //隐藏圆圈
            circleImageView.isHidden = true
        }else{
    //首页
            setupCircleImageViewAnim()
        }
    }
    //添加动画
    private func setupCircleImageViewAnim(){
    //实例化动画对象  keyPath 动画方法
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        //动画时间
        anim.duration = 10
        //toValue
        anim.toValue = Double.pi * 2
        //c重复次数
        anim.repeatCount = MAXFLOAT
        //切换界面 或者跳转界面是动画会停止
        anim.isRemovedOnCompletion = false
        //添加动画  forKey 动画起名字
        circleImageView.layer.add(anim, forKey: nil)
    }
    //视图
    func setupUI(){
    backgroundColor = RGB(r: 235, g: 235, b: 235)

        //添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(logoImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //设置约束
        //圆圈
        circleImageView.snp.makeConstraints { (
            make) in
            make.center.equalTo(self)
        }
        //遮挡
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //logo
        logoImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //文字
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp.bottom).offset(16)
        }
        //注册按钮
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
    //登录按钮
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.size.top.equalTo(registerButton)
        }
    }
    //懒加载控件
    //圆圈
    private lazy var circleImageView: UIImageView = UIImageView(named:"visitordiscover_feed_image_smallicon")
    //遮挡    
    private lazy var maskImageView: UIImageView = UIImageView(named:"visitordiscover_feed_mask_smallicon")
//    logo
    private lazy var logoImageView:UIImageView = UIImageView(named:"visitordiscover_feed_image_house")
    //文字
    private lazy var messageLabel:UILabel = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", textColor: UIColor.darkGray, fontSize: LYNormalFontSize, maxWidth: 230)
    //注册按钮
    private lazy var registerButton:UIButton = UIButton(title: "注册", fontSize: LYNormalFontSize, normalColor: UIColor.darkGray, highlightedColor: LYThemeCoclor, backgroundImgName: "common_button_white", target: self, action: #selector(loginClick))
    //登录按钮
    private lazy var loginButton:UIButton = UIButton(title: "登陆", fontSize: LYNormalFontSize, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange, backgroundImgName: "common_button_white", target: self, action: #selector(loginClick))
    
    @objc private func loginClick(){
    
        //03执行闭包
        closure?()
    }
}
